﻿// -----------------------------------------------------------------------
// <copyright file="Factory_Tests.cs" company="CompDJ">
//     Copyright (c) CompDJ. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace CompDJ.Web.Abstractions.UnitTests
{
    using System.IO;
    using System.Web;
    using CompDJ.Web.Abstractions;
    using FakeItEasy;
    using FluentAssertions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>A unit test class</summary>
    [TestClass]
    public class Factory_Tests
    {
        /// <summary>A unit test initialization method</summary>
        /// <remarks>Needed because we are using static classes for the factories</remarks>
        [TestInitialize]
        public void TestInitialize()
        {
            WebAbstractions.Factory.ResetAllFactories();
        }

        /// <summary>A unit test method</summary>
        [TestMethod, TestCategory("UnitTests"), TestCategory("HttpContextBase"),
        Description("HttpContextFactory.Current.Session.SessionID - Target should be expected")]
        public void HttpContextFactory_Current_Session_SessionID_ShouldBeExpected()
        {
            var expected = "1234567890";

            WebAbstractions.Factory.InitializeMock(null, null, null, h =>
                {
                    //// Mocking for HttpContextBase Here
                    A.CallTo(() => h.Session.SessionID).Returns(expected);
                });

            var target = HttpContextFactory.Current.Session.SessionID;

            target.Should().Be(expected);
        }

        /// <summary>A unit test method</summary>
        [TestMethod, TestCategory("UnitTests"), TestCategory("HttpContextBase"),
        Description("HttpContextFactory.Current - Context should not be null")]
        public void HttpContextFactory_Current_Context_ShouldNotBeNull()
        {
            HttpContext.Current = new HttpContext(
                new HttpRequest(
                    @"c:\Unit Tests\HttpContext.html", 
                    "http://unit-tests/", 
                    string.Empty), 
                new HttpResponse(TextWriter.Null));

            var target = HttpContextFactory.Current;

            target.Should().NotBeNull();
        }

        /// <summary>A unit test method</summary>
        [TestMethod, TestCategory("UnitTests"), TestCategory("FormsAuthenticationFactory"),
        Description("FormsAuthenticationFactory.Current.Authenticate('Unit', 'Test') - Target should return true")]
        public void FormsAuthenticationFactory_Current_Authenticate_Unit_Test_TargetShouldReturnTrue()
        {
            bool expected = true;

            WebAbstractions.Factory.InitializeMock(f =>
                {
                    //// Mocking for IFormsAuthentication Here
                    A.CallTo(() => f.Authenticate(A<string>.Ignored, A<string>.Ignored)).Returns(true);
                }, null, null, null);

            var target = FormsAuthenticationFactory.Current.Authenticate("Unit", "Test");

            target.Should().Be(expected);
        }

        /// <summary>A unit test method</summary>
        [TestMethod, TestCategory("UnitTests"), TestCategory("MembershipFactory"),
        Description("MembershipFactory.Current.Authenticate('Unit', 'Test') - Target should return true")]
        public void MembershipFactoryFactory_Current_ValidateUser_Unit_Test_TargetShouldReturnTrue()
        {
            bool expected = true;

            WebAbstractions.Factory.InitializeMock(null, m =>
                {
                    //// Mocking for IMembership Here
                    A.CallTo(() => m.ValidateUser(A<string>.Ignored, A<string>.Ignored)).Returns(true);
                }, null, null);

            var target = MembershipFactory.Current.ValidateUser("Unit", "Test");

            target.Should().Be(expected);
        }
        
        /// <summary>A unit test method</summary>
        [TestMethod, TestCategory("UnitTests"), TestCategory("ConfigurationManagerFactory"),
        Description("ConfigurationManagerFactory.Current.GetSection('UnitTest') - Target should return true")]
        public void ConfigurationManagerFactory_Current_GetSection_UnitTest_TargetShouldReturnTrue()
        {
            string expected = "Unit test";

            WebAbstractions.Factory.InitializeMock(null, null, c =>
                {
                    //// Mocking for IConfigurationManager Here
                    A.CallTo(() => c.GetSection(A<string>.Ignored)).Returns("Unit test");
                }, null);

            var target = ConfigurationManagerFactory.Current.GetSection("UnitTest");

            target.Should().Be(expected);
        }
    }
}
